package com.tony.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.tony.config.CacheConfig;
import com.tony.dao.TonyUserDao;
import com.tony.po.TonyUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * CREATE TABLE `tony_user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(64) DEFAULT NULL,
 `age` int(4) DEFAULT NULL,
 `user_name` varchar(64) DEFAULT NULL COMMENT '用户名称',
 `role_id` int(11) DEFAULT NULL COMMENT '用户角色',
 `create_date` datetime NULL DEFAULT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 */
@Controller
@RequestMapping("/beetl")
public class BeetlController {
    Logger logger = LoggerFactory.getLogger(BeetlController.class);
    @Autowired
    TonyUserDao tonyUserDao;
    @Autowired
    CacheManager cacheManager;

    /**
     * http://localhost:8010/beetl/add?age=11&roleId=1&name=test&userName=tony_test
     * @param tonyUser
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String insert(TonyUser tonyUser){
        logger.info("insert,{}",JSON.toJSONString(tonyUser));
        tonyUserDao.insert(tonyUser);
        return JSON.toJSONString(tonyUserDao.all());
    }

    /**
     * http://localhost:8010/beetl/modify?id=1&age=12&roleId=2&name=test1&userName=tony_test1
     * @param tonyUser
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public String update(TonyUser tonyUser){
        logger.info("update,{}",JSON.toJSONString(tonyUser));
        int id = tonyUserDao.updateById(tonyUser);
        return JSON.toJSONString(tonyUserDao.unique(id));
    }

    /**
     * http://localhost:8010/beetl/remove?id=1
     * @param tonyUser
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public String delete(TonyUser tonyUser){
        logger.info("delete,{}",JSON.toJSONString(tonyUser));
        int id = tonyUserDao.deleteById(tonyUser.getId());
        return JSON.toJSONString(tonyUserDao.all());
    }

    /**
     * http://localhost:8010/beetl/search?id=1
     * @param id
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public String select(Integer id){
        logger.info("select,{}",id);
        if(id==null){
            return JSON.toJSONString(tonyUserDao.all());
        }else{
            TonyUser tonyUser = getCatch(id);
            if(tonyUser!=null){
                return "缓存："+JSON.toJSONString(tonyUser);
            }
            return JSON.toJSONString(tonyUserDao.unique(id));
        }
    }

    public TonyUser getCatch(Integer id){
        Cache cache = cacheManager.getCache(id.toString());
        Cache.ValueWrapper valueWrapper = cache.get(id);
        if (valueWrapper!=null) {
            TonyUser tonyUser = (TonyUser) valueWrapper.get();
            return tonyUser;
        }
        return  null;
    }

    /**
     * http://localhost:8010/beetl/select?name=test
     * @param name
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    public String selectByName(String name){
        logger.info("",name);
        return JSON.toJSONString(tonyUserDao.selectByName(name), SerializerFeature.WriteNullListAsEmpty);
    }


}
